package com.dragon.client.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.auth.IExamineService;
import com.dragon.repository.domain.dto.ExamineDTO;
import com.dragon.repository.domain.vo.ExamineUserVO;
import com.dragon.repository.domain.vo.ExamineVO;
import com.dragon.repository.entity.Examine;
import com.dragon.repository.entity.ExamineUser;
import com.dragon.repository.entity.FromDetail;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 审批操作表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-06
 */
@RestController
@RequestMapping("examine")
@Api(tags = "审批管理")
public class ExamineController {

    @Autowired
    IExamineService service;


    @GetMapping("list")
    @ApiOperation(value = "获取申请或者审批详情")
    public ResponseData selectDetailsById(@RequestParam Integer id,@RequestParam Integer type){
        FromDetail list = service.selectDetailsById(id,type);
        return ResponseData.success(list);
    }

    @GetMapping("getList")
    @ApiOperation(value = "获取审批详细list")
    public ResponseData getDetailsList(@RequestParam Integer id, @RequestParam Integer type){
        List<Examine> detailsList = service.getExamineList(id, type);
        return ResponseData.success(detailsList);
    }

    @GetMapping("isAudit")
    @ApiOperation(value = "检测当前用户是否有审核的权限")
    public ResponseData<ExamineDTO> getUserIsAuditBy(
            @ApiParam(hidden = true) @Login Integer adminId,
            @ApiParam(name = "type",required = true,type = "int",value = "流程类型id") @RequestParam Integer type,
            @ApiParam(name = "mainId",required = true,type = "int",value = "主表id") @RequestParam Integer mainId){
        Examine userIsAuditBy = service.getUserIsAuditBy(adminId, type, mainId);
        return ResponseData.success( ExamineDTO.convert(userIsAuditBy));
    }

    // 数据权限控制不一样
    @GetMapping("applicantPageList")
    @ApiOperation(value = "申请分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "状态"),
            @ApiImplicitParam(name = "type",required = false,type = "int",value = "流程类型"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数")
    })
    public ResponseData<List<ExamineDTO>> selectApplicantPageList(
            @RequestParam(required = false) Integer status,@RequestParam(required = false) Integer type,
            @ApiParam(hidden = true) @Login Integer applicantId, @RequestParam Integer page, @RequestParam Integer limit){
        Page<Examine> pageResult = service.selectApplicantPageList(status,type,applicantId,page,limit);
        return ResponseData.success(ExamineDTO.convertList(pageResult.getRecords()), pageResult.getTotal());
    }

    @GetMapping("auditPageList")
    @ApiOperation(value = "审批分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",required = false,type = "int",value = "状态"),
            @ApiImplicitParam(name = "type",required = false,type = "int",value = "流程类型"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数")
    })
    public ResponseData<List<ExamineDTO>> selectAuditPageList(
            @RequestParam(required = false) Integer status,@RequestParam(required = false) Integer type,
            @ApiParam(hidden = true) @Login Integer auditId, @RequestParam Integer page, @RequestParam Integer limit
    ){
        Page<Examine> pageResult = service.selectAuditPageList(status,type,auditId,page,limit);
        return ResponseData.success(ExamineDTO.convertList(pageResult.getRecords()), pageResult.getTotal());
    }

    @PostMapping("audit")
    @ApiOperation(value = "审核")
    public  ResponseData audit(@RequestBody @Validated(Insert.class) ExamineUserVO vo){
        service.audit(ExamineUserVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("antiAudit")
    @ApiOperation(value = "反审核操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "streamType", value = "流程id", required = true, type = "int"),
            @ApiImplicitParam(name = "correlationId", value = "主表id", required = true, type = "int")
    })
    public ResponseData auditOperation(@RequestParam Integer streamType,
                                       @RequestParam Integer correlationId ){
        service.antiAuditOperation(streamType, correlationId);
        return ResponseData.success();
    }


    @PostMapping("addOperation")
    @ApiOperation(value = "添加修改申请")
    public ResponseData addOperation(@RequestBody @Validated(Insert.class) ExamineVO vo){
        service.addOperation(ExamineVO.convert(vo));
        return ResponseData.success();
    }


    @PostMapping("addSign")
    @ApiOperation(value = "添加加签")
    public ResponseData addSign(@RequestBody @Validated(Insert.class) ExamineVO vo){

        service.addSign(ExamineVO.convert(vo));
        return ResponseData.success();
    }


}
